<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;

class News extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($token)
    {

//        dump($token);
        $uid = DB('business')
                ->field('id')
                ->where('token',$token)
                ->find();
//        dump($uid['id']);
        if($uid) {
            $res = DB::view('message', ['session', 'title', 'id','time'])
                ->view('mes_bus', 'status', 'sid=message.id')
                ->where('mes_bus.bid', $uid['id'])
                ->order('time','desc')
                ->select();

            //查询未读消息条数
            $unread = DB('mes_bus')
                    ->where([
                        'status'=>2,
                        'bid'=>$uid['id']
                    ])
                    ->count('sid');

            $arr['wdcount'] = $unread;
            $arr['data'] = $res;

            return (json_encode($arr));
        }
        $this->error('请求失败');

    }

    public function doStatus($token)
    {

        $uid = DB('business')
            ->field('id')
            ->where('token',$token)
            ->find();
        if($uid) {
            $res = DB('mes_bus')
                ->where('mes_bus.bid', $uid['id'])
                ->update([
                    'status' => '1'
                ]);
            return;
        }
        $this->error('请求失败');
    }
}
